<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="答题人：">
                        <Input v-model="search.username"></Input>
                    </FormItem>

                    <FormItem label="答题时间：">
                        <DatePicker style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                    </FormItem>
                </Form>

            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>

    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    export default {
        name: 'question-result',
        data() {
            return {
                loading: false,

                // 搜索对象
                search: {
                    username: '',
                    start_time: '',
                    end_time: '',
                    page: 1,
                    rows: 10
                },

                // 数据总条数
                total: 0,

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '答题人',
                        align: 'center',
                        key: 'username'
                    },
                    {
                        title: '答题得分',
                        align: 'center',
                        key: 'score'
                    },
                    {
                        title: '答题赠分',
                        align: 'center',
                        key: 'zeng_score'
                    },
                    {
                        title: '答题时间',
                        align: 'center',
                        key: 'start_time'
                    }
                ],
                data1: []
            }
        },
        methods: {
            // 获取答题明细
            getQuestionResult() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-class_learn_record-ajax_get_clr', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getQuestionResult()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getQuestionResult()
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.getQuestionResult()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getQuestionResult()
            },
        },
        created() {
            this.getQuestionResult()
        }
    }
</script>

<style lang='less' scoped>
    .search {
        .search_box {
            display: flex;

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>